package com.fc.test.service;

import com.fc.test.mapper.custom.TEventsTurntableDao;
import com.fc.test.mapper.custom.TUserDao;
import com.fc.test.model.auto.TsysPremission;
import com.fc.test.model.custom.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * describe: 活动用户操作相关接口
 *
 * @author xugang
 * @date 2018/9/19 21:53
 */
@Service
public class UserService {

    @Autowired
    private TUserDao tUserDao;

    @Autowired
    private TEventsTurntableDao tEventsTurntableDao;

    /**
     * 添加用户
     *
     * @param mobilePhone 手机号码
     * @param username    用户姓名
     * @param password    登录密码
     * @param email       电子邮件
     * @param gender      性别(0女；1男)
     * @param age         年龄
     * @return 1成功；0失败；
     */
    @Transactional
    public int save(String mobilePhone, String username, String password, String email, int gender, int age) {
        TUser tUser = new TUser();
        tUser.setMobilePhone(mobilePhone);
        tUser.setUsername(username);
        tUser.setPassword(password);
        tUser.setEmail(email);
        tUser.setGender(gender);
        tUser.setAge(age);
        tUser.setRegisterTime(new Date());
        tUserDao.saveUser(tUser);
        return 1;
    }

    /**
     * 添加用户
     *
     * @param tUser
     * @return
     */
    @Transactional
    public int save(TUser tUser) {
        tUserDao.saveUser(tUser);
        return 1;
    }


    /**
     * 保存用户获奖记录
     *
     * @param mobilePhone 手机号码
     * @param username    用户姓名
     * @param prize       中奖内容
     * @return
     */
    @Transactional
    public int saveWinningRecords(String mobilePhone, String username, String prize) {
        TEventsTurntable turntable = new TEventsTurntable();
        turntable.setMobilePhone(mobilePhone);
        turntable.setUsername(username);
        turntable.setPrize(prize);
        turntable.setAwardTime(new Date());
        tEventsTurntableDao.saveWinningRecords(turntable);
        return 1;
    }

    /**
     * 保存用户获奖记录
     *
     * @param turntable
     */
    @Transactional
    public int saveWinningRecords(TEventsTurntable turntable) {
        tEventsTurntableDao.saveWinningRecords(turntable);
        return 1;
    }

    /**
     * 根据手机号码查询用户
     *
     * @param mobilePhone
     * @return
     */
    public TUser queryByMobilePhone(String mobilePhone) {
        TUser tUser = tUserDao.queryByMobilePhone(mobilePhone);
        return tUser;
    }

    /**
     * 根据手机号码查询用户获奖记录
     *
     * @param mobilePhone
     * @return
     */
    public TEventsTurntable queryRecordsByMobilePhone(String mobilePhone) {
        TEventsTurntable turntable = tEventsTurntableDao.queryRecordsByMobilePhone(mobilePhone);
        return turntable;
    }

    /**
     * 活动用户信息分页查询
     *
     * @param tablepar  分页条件
     * @param searchTxt 查询条件
     * @return
     */
    public PageInfo<TUser> list(Tablepar tablepar, String searchTxt) {
        TUserExample tUserExample = new TUserExample();
        tUserExample.setOrderByClause("id+0 ASC");
        if (searchTxt != null && !"".equals(searchTxt)) {
            tUserExample.createCriteria().andUsernameLike("%" + searchTxt + "%");
        }

        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
        List<TUser> list = tUserDao.selectByExample(tUserExample);
        PageInfo<TUser> pageInfo = new PageInfo<TUser>(list);
        return pageInfo;
    }

    /**
     * 活动用户中奖信息分页查询
     *
     * @param tablepar  分页条件
     * @param searchTxt 查询条件
     * @return
     */
    public PageInfo<TEventsTurntable> list4turntable(Tablepar tablepar, String searchTxt) {
        TEventsTurntableExample tEventsTurntableExample = new TEventsTurntableExample();
        tEventsTurntableExample.setOrderByClause("id+0 ASC");
        if (searchTxt != null && !"".equals(searchTxt)) {
            tEventsTurntableExample.createCriteria().andUsernameLike("%" + searchTxt + "%");
        }

        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
        List<TEventsTurntable> list = tEventsTurntableDao.selectByExample(tEventsTurntableExample);
        PageInfo<TEventsTurntable> pageInfo = new PageInfo<TEventsTurntable>(list);
        return pageInfo;
    }

}
